<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="班级名称" prop="cname">
       <el-input v-model="dataForm.cname" placeholder="班级名称"></el-input>
      </el-form-item>
      <el-form-item label="所属院校">
				<el-select v-model="dataForm.sid" placeholder="所属院校">
					<el-option label="--所属院校--" value=""></el-option>
					<el-option v-for="s in schoolList" :key="s.id" :label="s.name" :value="s.id"></el-option>
				</el-select>
			</el-form-item>
      <el-form-item label="教员">
				<el-select v-model="dataForm.jy_id" placeholder="教员">
					<el-option label="--教员--" value=""></el-option>
					<el-option v-for="t in teacherList" :key="t.id" :label="t.account" :value="t.id"></el-option>
				</el-select>
			</el-form-item>
       <el-form-item label="辅导员">
				<el-select v-model="dataForm.fdy_id" placeholder="辅导员">
					<el-option label="--辅导员--" value=""></el-option>
					<el-option v-for="t in teacherList" :key="t.id" :label="t.account" :value="t.id"></el-option>
				</el-select>
			</el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          id: 0,
          cid: 0,
          cname: '',
          sid: '',
          sname: '',
          fdy_id: '',
          fdy_namne: '',
          jy_id: '',
          jy_namne: ''
        },
        schoolList: [],
        teacherList: []
      }
    },
    methods: {
      init (id,row,schoolList) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        });
       
        if(this.dataForm.id != 0) {
          const cname = row.cname;
          this.$http({
            url: this.$http.adornUrl(`/sys/classes/getClassByParams`),
            method: 'get',
            params: this.$http.adornParams(
              {cname},false
            )
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data.data.list);
              this.dataForm = data.data.list[0];
            }
         })
        } else {
           //this.dataForm = {}
        }
        this.schoolList = schoolList || [];
        this.$http({
					url: this.$http.adornUrl('/sys/classes/getTeacherAll'),
					method: 'get',
					params: this.$http.adornParams({},false),
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						this.teacherList = data.data;
					} else {
						this.teacherList = []
					}
					this.dataListLoading = false
				})
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log(this.dataForm);
            this.$http({
              url: this.$http.adornUrl(`/sys/classes/${this.dataForm.id == 0 ? 'addClasses' : 'updateClasses'}`),
              method: 'post',
              data: this.$http.adornData(
                {
                  'id': this.dataForm.cid || undefined,
                  'name': this.dataForm.cname,
                  'teacher_id': this.dataForm.jy_id,
                  'instructorr_id': this.dataForm.fdy_id,
                  'school_id': this.dataForm.sid
                },
                false
              )
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
